package com.lagou.edu.controller;

import com.lagou.edu.pojo.Resume;
import com.lagou.edu.service.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class ResumeController {

    /**
     * Spring容器和SpringMVC容器是有层次的（父子容器）
     * Spring容器：service对象+dao对象
     * SpringMVC容器：controller对象，，，，可以引用到Spring容器中的对象
     */


    @Autowired
    private ResumeService resumeService;

    @PostMapping("/save")
    public  ModelAndView save(Resume resume) throws Exception {
        ModelAndView modelAndView=new ModelAndView() ;
        resumeService.save(resume);
        modelAndView.addObject("list",resumeService.showList()) ;
        modelAndView.setViewName("list.jsp");
        return  modelAndView ;
    }

    @PostMapping("/update")
    public  ModelAndView update(Resume resume) throws Exception {
        ModelAndView modelAndView=new ModelAndView() ;
        resumeService.update(resume);
        modelAndView.addObject("list",resumeService.showList()) ;
        modelAndView.setViewName("list.jsp");
        return  modelAndView ;
    }

    @GetMapping("delete")
    public ModelAndView delete(Integer id){
        ModelAndView modelAndView=new ModelAndView() ;
        resumeService.delete(id);
        modelAndView.addObject("list",resumeService.showList()) ;
        modelAndView.setViewName("list.jsp");
        return  modelAndView ;
    }

    @GetMapping("/queryAll")
    public ModelAndView  queryAll() throws Exception {
        ModelAndView modelAndView=new ModelAndView() ;
        modelAndView.addObject("list",resumeService.showList()) ;
        modelAndView.setViewName("list.jsp");
        return  modelAndView ;
    }
    @GetMapping("/show")
    public  ModelAndView show(int id) throws Exception {
        ModelAndView modelAndView=new ModelAndView() ;
        Resume resume=resumeService.show(id) ;
        modelAndView.addObject("resume",resume) ;
        modelAndView.setViewName("update.jsp");
        return  modelAndView ;

    }
}
